//
//  BKSearchField.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/30.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKSearchField.h"
#import "BKSearchPickViewDataSource.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>

@interface BKSearchField()


@end

@implementation BKSearchField


- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder{
    
    self = [super awakeAfterUsingCoder:aDecoder];
    if (self) {
        
        self.layer.borderColor = [[UIColor whiteColor] CGColor];
        self.layer.borderWidth = 1.5;
        self.textColor = [UIColor whiteColor];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 30)];
        textLabel.text = self.placeholder;
        textLabel.font = [UIFont systemFontOfSize:14];
        textLabel.textColor= [UIColor whiteColor];
        self.leftView = textLabel;
        
        self.leftViewMode = UITextFieldViewModeAlways;
        
        self.textAlignment = NSTextAlignmentRight;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_down"]];
        self.rightViewMode = UITextFieldViewModeAlways;
        
        
        UIColor *color = [UIColor clearColor];
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: color}];
        if (![self.placeholder hasPrefix:@"排"] && ![self.placeholder isEqualToString:@"考分"]&& ![self.placeholder isEqualToString:@"准考证"]) {
           
                self.rightView = imageView;
            //此段代码可以设置 placeHolder颜色
        }
        if( [self.placeholder isEqualToString:@"考分"] ) {
            [super setKeyboardType:UIKeyboardTypeNumberPad];
        }
       
        
    }
    return self;
    
    
}



- (void)setFlag:(NSInteger)flag{
    _flag = flag;
    
    UIPickerView *picker = [[UIPickerView alloc] init];
    picker.tag = _flag;
    _pickViewDataSource = [[BKSearchPickViewDataSource alloc] init];
    picker.dataSource = _pickViewDataSource;
    picker.delegate = _pickViewDataSource;
    if (flag == 8004) {//大学初始化
        _pickViewDataSource.colleageArray = _colleageArray;
    }
    
    if (flag == 8011) {//预留数据初始化
        _pickViewDataSource.dataArray = _dataArray;
        
    }
    UIToolbar *accessoryView = [[UIToolbar alloc] init];
    accessoryView.frame=CGRectMake(0, 0, 320, 38);
    accessoryView.backgroundColor = [UIColor grayColor];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    @weakify(self);
    doneBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        NSInteger row = [picker selectedRowInComponent:0];
        switch (picker.tag) {
            case KAOQU:
                self.text = self.pickViewDataSource.provArray[row];
                break;
            case ZHUANYE:
                self.text = self.pickViewDataSource.typeArray[row];
                break;
            case PICI:
                self.text = self.pickViewDataSource.piciArray[row];
                break;
            case WENLI:
                self.text = self.pickViewDataSource.wenliArray[row];
                break;
            case 8004:
                // 省内大学
                self.text = self.colleageArray[row][@"name"];
                break;
            case JIANGXUEJIN:
                self.text = self.pickViewDataSource.jxjArray[row];
                break;
            case FEILEI:
                self.text = self.pickViewDataSource.feileiArray[row];
                break;
            case ZHIWEI:
                self.text = self.pickViewDataSource.zhiweiArray[row];
                break;
   
            case BANJI:
                self.text = self.pickViewDataSource.banji[row];
                break;
            case JIESHU:
                self.text = self.pickViewDataSource.year[row];
                break;
            case BUZHU:
                self.text = self.pickViewDataSource.buzhuArray[row];
                break;
                case QITA:
                self.text = self.pickViewDataSource.dataArray[row];
                break;
            case BENZHUN:
                self.text = self.pickViewDataSource.benzhuanArray[row];
                break;
            case MIANFEI:
                self.text = self.pickViewDataSource.mianfeiArray[row];
                break;
            default:
                break;
        }
        [self endEditing:YES];
        return [RACSignal empty];
        
    }];
    [doneBtn setTintColor:[UIColor blackColor]];
    UIBarButtonItem *spaceBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    accessoryView.items=@[spaceBtn,doneBtn];
    self.inputAccessoryView = accessoryView;
    self.inputView = picker;


    
}
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
    return CGRectMake(5, 7, 80, 30);

}
- (CGRect)rightViewRectForBounds:(CGRect)bounds{

    return CGRectMake(bounds.size.width-30, 13, 20, 20);
    
}



- (CGRect)placeholderRectForBounds:(CGRect)bounds{
    return CGRectMake(0, 0, bounds.size.width-40, bounds.size.height);
}

- (CGRect)textRectForBounds:(CGRect)bounds{

    return CGRectMake(0,0, bounds.size.width-30, bounds.size.height);
    
}

- (CGRect)editingRectForBounds:(CGRect)bounds{

    return CGRectMake(0, 0, bounds.size.width-40, bounds.size.height);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
